<?php
/**
 * 教室信息类
 */
namespace app\admin\controller;

use app\admin\model\Classifymodel;
use think\App;
use think\facade\View;
use app\admin\model\Classroom;
use app\admin\model\Teachermodel;
use app\admin\model\Contentmodel;
use app\admin\model\Classhourmodel;
use app\admin\validate\Content as validate;

class Classhour extends Common
{
    protected $id;
    protected $cfmodel;
    protected $croomModel;
    protected $teacherModel;
    protected $contentModel;

    public function __construct(App $app)
    {
        $this->validate = validate::class;
        $this->validateType = 'classhour';
        $this->localModel = new Classhourmodel();
        parent::__construct($app);
        $this->cfmodel = new Classifymodel();
        $this->croomModel = new Classroom();
        $this->teacherModel = new Teachermodel();
        $this->contentModel = new Contentmodel();
        $this->id = $this->request->param('id', 0);
    }

    public function index()
    {
        $dataArr = $this->request->request('search/a', []);

        if($dataArr['title'])         $where[] = ['title', 'like', "%".$dataArr['title']."%"];
        if($dataArr['username'])      $where[] = ['username', 'like', "%".$dataArr['username']."%"];
        if($dataArr['status'])        $where[] = ['status', '=', $dataArr['status']];
        if($dataArr['crid'])          $where[] = ['crid', '=', $dataArr['crid']];
        if($dataArr['start_time'])    $where[] = ['start_time', '>=', $dataArr['start_time']];
        if($dataArr['end_time'])      $where[] = ['start_time', '<=', $dataArr['end_time']];

        $list = $this->localModel->findAllPageInfo('*', $where);
        $showData['croomData'] = $this->croomModel->findAllInfo('id, name', [['status', '=', 1]]);
        View::assign(['data'=>$list['data'], 'pageData'=>$list['page'], 'showData'=>array_merge($dataArr, $showData)]);
        return view();
    }

    public function add(){
        if(!session('adminUserInfo.cid')){
            $showData['croomData'] = $this->croomModel->findAllInfo('id, name', [['status', '=', 1]]);
        }

        $showData['contentData'] = $this->contentModel->findAllInfo('id, title', [['status', '=', 1], ['examine_status', '=', 9], ['type', '=', 6]]);
        $showData['ctypeData'] = $this->cfmodel->findAllInfo('id, name', [['status', '=', 1], ['level', '=', 2], ['fid', '=', 18]]);

        if($this->id){
            $showData['id'] = $this->id;
            $showData['detail'] = $this->localModel->findDetaiInfoById('*', $this->id);
            $showData['courseDetail'] = $this->localModel->findCourseDetailById($this->id);
            $showData['student'] = $this->localModel->getStudentsList($this->id);
            $teacherData = $this->teacherModel->findAll([['u.status', '=', 1], ['u.cid', '=', $showData['detail']['crid']]], $this->request->param(), 100);
            $showData['start'] = 100;
            $showData['teacherData'] = $teacherData['data'];
            View::assign($showData);
            return view('edit');
        }else{
            if(!session('adminUserInfo.cid')){
                $teacherData = $this->teacherModel->findAll([['u.status', '=', 1], ['u.cid', '=', session('adminUserInfo.cid')]], $this->request->param(), 100);
                $showData['teacherData'] = $teacherData['data'];
            }
            View::assign($showData);
            return view();
        }
    }

    public function save(){
        $dataArr = $this->request->request('info/a', []);
        $otherArr = $this->request->request('other/a', []);
        $detailArr = $this->request->request('detail/a', []);

        //if($msg = $this->checkData($dataArr, $this->validateType))  return json(['result'=>'fail', 'title'=>$msg]);
        if(!count($otherArr))  return json(['result'=>'fail', 'title'=>'请选择学员！']);

        $res = $this->localModel->saveClassHourInfo($dataArr, $otherArr, $detailArr, $this->id);

        if(!$res)  return json(['result'=>'fail', 'title'=>'操作失败！']);

        $this->logsData = ['cid'=>$this->id ? $this->id : $res, 'action'=>$this->id ? 'edit' : 'add', 'msg'=>$dataArr['title']];
        return json(['result'=>'success', 'title'=>'恭喜您操作成功！', 'url'=>(string)url($this->request->controller().'/index')]);
    }

}
